package com.ocss.web.controller.system;

import java.util.List;

import com.ocss.common.core.domain.AjaxResult;
import com.ocss.system.service.IOcssCourseSelectionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ocss.common.core.controller.BaseController;
import com.ocss.system.domain.OcssCourse;
import com.ocss.common.core.page.TableDataInfo;


/**
 * 选课信息列表
 *
 * @author ligen
 * @date 2024-03-07
 */
@RestController
@RequestMapping("/system/course_selection")
public class OcssCourseSelectionController extends BaseController {


    @Autowired
    private IOcssCourseSelectionService ocssCourseSelectionService;

    /**
     * 查询课程信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:course_selection:list')")
    @GetMapping("/list")
    public TableDataInfo list(OcssCourse ocssCourse) {
        startPage();
        List<OcssCourse> list = ocssCourseSelectionService.selectStudentCourseSelectionList(ocssCourse);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:course_selection:select')")
    @PutMapping("/robClasses")
    public AjaxResult robClasses(@RequestBody OcssCourse ocssCourse) {

        int row = ocssCourseSelectionService.robClasses(ocssCourse);

        if (row > 0) {
            return success("抢课成功");
        }

        if (row == -1) {
            return error("抢课失败，课程已抢完！");
        }

        return error("你已抢过改课程，无需重复抢课");
    }

    /**
     * 查询课程信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:course_selection:list')")
    @GetMapping("/listSelectCourse")
    public TableDataInfo selectedList(OcssCourse ocssCourse) {
        startPage();
        List<OcssCourse> list = ocssCourseSelectionService.selectStudentCourseSelectedList(ocssCourse);
        return getDataTable(list);
    }

    // 退课
    @PreAuthorize("@ss.hasPermi('system:course_selection:retreat')")
    @PutMapping("/retreat")
    public AjaxResult retreat(@RequestBody OcssCourse ocssCourse) {

        int row = ocssCourseSelectionService.retreat(ocssCourse);

        if (row > 0) {
            return success("退课成功");
        }

        if (row == -2) {
            return error("课程选课已结束，无法退课！");
        }

        return error("课程不存在！");
    }

}
